package com.gpipe.dataTransfer;

import com.gpipe.dirwatcher.AbstractResourceWatcher;
import com.gpipe.dirwatcher.DirectoryWatcher;
import com.gpipe.dirwatcher.FileListener;
import com.gpipe.dirwatcherRealtime.Conf;
import com.gpipe.dirwatcherRealtime.DirectorySnapshotRealtime;
import com.gpipe.tools.GpipeInit;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by admin on 2017/9/23.
 */
public class Entrance extends AbstractResourceWatcher {
    /**
     * The current map of files and their timestamps (String fileName => Long
     * lastMod)
     */
    private Map currentFiles = new HashMap();

    /**
     * The directory to watch.
     */
    private String directory;

    /**
     * The map of last recorded files and their timestamps (String fileName =>
     * Long lastMod)
     */
    private Map prevFiles = new HashMap();
    /**
     * Constructor that takes the directory to watch.
     *
     * @param intervalSeconds The interval to use when monitoring this
     *                        resource.  I.e., ever x seconds, check this resource to see
     *                        what has changed.
     * @param directoryPath            the directory to watch
     */
    public Entrance(int intervalSeconds, String directoryPath) {
        super(intervalSeconds, directoryPath + " interval watcher.");
    }

    public static void main(String[] args) {

        // local test
//        args = new String[]{"E:\\testLog\\jw_test_file\\realtime", "4"};

        // Monitor c:/temp every 5 seconds
        if (args.length != 2) {
            System.err.println("useage <dirpath> <interval>");
            System.exit(0);
        }

        String dirPath = args[0];
        String interval = args[1];

        Entrance entrance = new Entrance(Integer.parseInt(interval),dirPath);
        entrance.start();

    }


    @Override
    protected void doInterval() {



    }

    /**
     * Store the file names and the last modified timestamps of all the files
     * and directories that exist in the directory at this moment.
     * 支持两层目录的识别，数据文件存在在第二层目录下。第一层目录用来区分不同的表数据
     */
    private void takeSnapshot() {
        // Set the last recorded snap shot to be the current list
        prevFiles.clear();
//        DirectorySnapshotRealtime.fileSnapshot.clear();
//        DirectorySnapshotRealtime.fileSnapshot.putAll(currentFiles);
        prevFiles.putAll(currentFiles);
        // And get a new current state with all the files and directories
        currentFiles.clear();
        File theDirectory = new File(directory);
        File[] files = theDirectory.listFiles();
        if (files != null && files.length > 0) {
            //第一层目录
            for(File file:files){
                if(file.isDirectory()){
                    //第二层目录
                    File[] files1 = file.listFiles();
                    for(File file1:files1){
                        //剔除没有加载完的文件
                        if(file1.isFile()&&!file1.getName().contains(".tmp")){
                            currentFiles.put(file1.getAbsolutePath(),new Long(file1.lastModified()));
                        }
                    }
                }
            }
        }
    }
}
